package de.cbf.cam.dsa.exchange.ownxml.descriptions;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import de.cbf.cam.dsa.model.ModelBase;
import de.cbf.cam.dsa.model.entities.OneAttribute;
import de.cbf.cam.dsa.model.entities.OneNPC;
import de.cbf.cam.dsa.model.entities.OneTalent;
import de.cbf.cam.dsa.model.entities.SF;
import de.cbf.cam.dsa.model.entities.WeaponTalent;
import de.cbf.cam.dsa.model.entities.items.Item;

/**
 * Class for the quick Im/Export of character data via XMLBeans
 * @author Stefan Alter
 *
 */
/**
 * @author Stefan Alter
 *
 */
/**
 * @author Stefan Alter
 *
 */
/**
 * @author Stefan Alter
 *
 */
public class CharacterDescription implements Serializable{
	/** */
	private static final long serialVersionUID = 4833516307532173720L;
	
	private String name;
	
	private List<AttributeDescription> 	attributes 	= new ArrayList<AttributeDescription>();	
	private List<TalentDescription> 	talents 	= new ArrayList<TalentDescription>();	
	private List<Item> 					items		= new ArrayList<Item>();
	private List<String> 				sfNames		= new ArrayList<String>();
	
	
	public CharacterDescription() {
		super();		
	}

	
	public CharacterDescription(OneNPC character){
		super();
		//base data
		name = character.getName();
		//attributes
		Collection<OneAttribute> attributes = character.getAttributeMap().values();
		for (Iterator<OneAttribute> iterator = attributes.iterator(); iterator.hasNext();) {
			OneAttribute oneAttribute = (OneAttribute) iterator.next();
			AttributeDescription attributeDescription = new AttributeDescription();
			attributeDescription.setName(oneAttribute.getName());
			attributeDescription.setValue(oneAttribute.getCurrentValue());
			this.attributes.add(attributeDescription);
		}
		
		//talents
		Collection<OneTalent> talents = character.getTalents();
		for (Iterator<OneTalent> iterator = talents.iterator(); iterator.hasNext();) {
			OneTalent oneTalent = (OneTalent) iterator.next();
			if (oneTalent instanceof WeaponTalent) {
				WeaponTalent weaponTalent = (WeaponTalent) oneTalent;
				WeaponTalentDescription desc = new WeaponTalentDescription();
				desc.setName(weaponTalent.getName());
				desc.setValue(weaponTalent.getTalValue());
				desc.setAtDistribution(weaponTalent.getAtDistribution());
				desc.setPaDistribution(weaponTalent.getPaDistribution());
				continue;
			}
			TalentDescription desc = new TalentDescription();
			desc.setName(oneTalent.getName());
			desc.setValue(oneTalent.getTalValue());			
		}		
		
		//items
		this.items = character.getItems();
		//sfNames
		Collection<SF> sfs = character.getSFs();
		for (Iterator<SF> iterator = sfs.iterator(); iterator.hasNext();) {
			SF sf = (SF) iterator.next();
			this.sfNames.add(sf.getName());			
		}
	}

	/**
	 * method to get the Character object from this description<br>
	 * reversal of constructor
	 * @return
	 */
	public OneNPC getCharacter(){
		OneNPC character = new OneNPC();
		//take over values
		//base data
		character.setName(name);
		
		// items		
		for (Iterator<Item> iterator = items.iterator(); iterator.hasNext();) {
			Item item = (Item) iterator.next();
			character.addItem(item);
		}
		
		// sfs		
		for (Iterator<String> iterator = sfNames.iterator(); iterator.hasNext();) {
			String sfName = (String) iterator.next();
			SF sf = ModelBase.getInstance().getSF(sfName);
			character.addSF(sf);
		}
		
		//talents	
		for (Iterator<TalentDescription> iterator = talents.iterator(); iterator.hasNext();) {
			TalentDescription talentDescription = (TalentDescription) iterator.next();			
			if (talentDescription instanceof WeaponTalentDescription) {
				WeaponTalentDescription weaponTalentDescription = (WeaponTalentDescription) talentDescription;
				character.addWeaponTalent(	weaponTalentDescription.getName(),
											weaponTalentDescription.getValue(),
											weaponTalentDescription.getAtDistribution(),
											weaponTalentDescription.getPaDistribution());									
				continue;
			}			
			character.addTalent(talentDescription.getName(),
								talentDescription.getValue());
		}
		
		//attributes		
		HashMap<String, OneAttribute> attributeMap = new HashMap<String, OneAttribute>();
		for (Iterator<AttributeDescription> iterator = attributes.iterator(); iterator.hasNext();) {
			AttributeDescription attributeDescription = (AttributeDescription) iterator.next();
			OneAttribute attribute = new OneAttribute(character, attributeDescription.getName());		
			attribute.setStartValue(attributeDescription.getValue());
			attribute.setCurrentValue(attributeDescription.getValue());
			attributeMap.put(attribute.getName(),attribute);
		}
		character.setAttributeMap(attributeMap);	
		return character;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<AttributeDescription> getAttributes() {
		return attributes;
	}

	public void setAttributes(List<AttributeDescription> attributes) {
		this.attributes = attributes;
	}

	public List<String> getSfNames() {
		return sfNames;
	}

	public void setSfNames(List<String> sfs) {
		this.sfNames = sfs;
	}
	public List<TalentDescription> getTalents() {
		return talents;
	}

	public void setTalents(List<TalentDescription> talents) {
		this.talents = talents;
	}

	public List<Item> getItems() {
		return items;
	}

	public void setItems(List<Item> items) {
		this.items = items;
	}
	
}
